/*
 *  fsutils.c
 *
 * (C) 2007 by Greg Hedlund
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "fsutils.h"
#include <stdio.h>
#include <CoreServices/CoreServices.h>
#include <sys/mount.h>

#include <stdio.h>

/* ping disk arbitration */
static int ping_diskarb(char* path)
{
	int ret;
    struct statfs sb;
    enum {
        kDiskArbDiskAppearedEjectableMask   = 1 << 1,
        kDiskArbDiskAppearedWholeDiskMask   = 1 << 2,
        kDiskArbDiskAppearedNetworkDiskMask = 1 << 3
    };
	
	printf("Ping Diskarg for %s", path);

    ret = statfs(path, &sb);
    if (ret < 0) {
        return ret;
    }

    if (!DiskArbInit || !DiskArbDiskAppearedWithMountpointPing_auto) {
        return 0;
    }

    ret = DiskArbInit();

    /* we ignore the return value from DiskArbInit() */

    ret = DiskArbDiskAppearedWithMountpointPing_auto(
              sb.f_mntfromname,
              (kDiskArbDiskAppearedEjectableMask |
               kDiskArbDiskAppearedNetworkDiskMask),
              path);

    /* we ignore the return value from the ping */

    return 0; 
}

/* Set the kHasCustomIcon flag on the specified folder */
OSStatus set_custom_icon_flag(char *path)
{
	FSRef ref;
	OSStatus st = FSPathMakeRef((const UInt8*)path, &ref, 0);
	if (st != KERN_SUCCESS)
		return st;
	FSCatalogInfo info;
	st = FSGetCatalogInfo(&ref, kFSCatInfoFinderInfo, &info, 0, 0, 0);
	if (st != KERN_SUCCESS)
			return st;
	((FolderInfo*)info.finderInfo)->finderFlags = kHasCustomIcon;
	st = FSSetCatalogInfo(&ref, kFSCatInfoFinderInfo, &info);
	if (st != KERN_SUCCESS)
			return st;
	ping_diskarb(path);

	return KERN_SUCCESS; 
}

void perform_icon_setup(void* data) {
	char *path = (char*)data;
	
	// wait for path to mount
	int cycleindex = 0;
	while(!is_directory_in_fstab(path)) {
		fprintf(stderr, "Cycle %d\n", cycleindex++);
		//sleep(1);
	}
	
	set_custom_icon_flag(path);
}

bool is_directory_in_fstab(char *path) {
	int numMountedFilesystems =
		getfsstat(NULL, 0, MNT_NOWAIT);
	
	if(numMountedFilesystems < 0) {
		fprintf(stderr, "Could not get number of mounted filesystems.");
		return FALSE;
	}
	
	struct statfs *buf = (struct statfs*)malloc(numMountedFilesystems*sizeof(struct statfs));
	
	if(buf == NULL) {
		fprintf(stderr, "Could not allocate memory for 'struct statfs *buf'.");
		return FALSE;
	}
	
	int retVal = 
		getfsstat(buf, numMountedFilesystems, MNT_NOWAIT);
	
	if(retVal < 0) return FALSE;
	
	int i = 0;
	for(i = 0; i < retVal; i++) {
		struct statfs currentFilesystem = buf[i];
		
		if(strcmp(path, currentFilesystem.f_mntonname) == 0) {
			free(buf);
			return TRUE;
		}
	}
	
	free(buf);
	return FALSE;
}

void set_icon_and_ping_diskarb(pthread_t *thread, char *path) 
{
//	pthread_t thread;
	pthread_create(thread, NULL, perform_icon_setup, (void*)path);	
//	return &thread;
}
